<template>
  <div class="app-container">
    <el-row type="flex" align="center" :gutter="20">
      <el-form class="filterData-block" label-position="left" :inline="true" label-width="auto">
        <el-form-item label="关键字:" label-width="62px" style="width: 170px">
          <el-input
            placeholder="姓名"
            v-model="filterData.realName"
            maxlength="20"
            size="small"
            clearable
            style="width: 100px"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="身份证:" label-width="62px" style="width: 170px">
          <el-input
            placeholder="身份证"
            v-model="filterData.idCard"
            maxlength="20"
            size="small"
            clearable
            style="width: 100px"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="民族:" label-width="62px" style="width: 170px">
          <el-cascader
             v-model="filterData.nationId"
            clearable
            placeholder="全部民族"
            :options="nationOption"
            size="small"
            :props="{value:'id',label:'name'}"
            filterDataable
             style="width: 100px"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="学历:" label-width="62px" style="width: 190px">
          <el-cascader
            v-model="filterData.educationId"
            clearable
            placeholder="全部学历"
            :options="educationOption"
            size="small"
            :props="{value:'id',label:'data'}"
            filterDataable
            style="width: 120px"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="从业状态:" label-width="80px" style="width: 190px">
          <el-cascader
            v-model="filterData.practiceState"
            clearable
            placeholder="全部状态"
            :options="practiceStateOption"
            size="small"
            :props="{value:'id',label:'name'}"
            filterDataable
            style="width: 100px"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="就业意愿:" label-width="80px" style="width: 190px">
          <el-cascader
            v-model="filterData.wishWork"
            clearable
            placeholder="全部"
            :options="wishWorkOption"
            size="small"
            :props="{value:'id',label:'name'}"
            filterDataable
            style="width: 100px"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="户籍类型:" label-width="80px" style="width: 190px">
          <el-cascader
            v-model="filterData.household"
            clearable
            placeholder="全部"
            :options="householdOption"
            size="small"
            :props="{value:'id',label:'name'}"
            @change="householdChange"
            filterDataable
            style="width: 100px"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="乡镇、村、组:" v-if="filterData.household&&filterData.household[0]===2" label-width="110px" style="width: 220px">
          <el-cascader
            v-model="filterData.groups"
            clearable
            placeholder="全部"
            :options="groupOption"
            size="small"
            :props="{value:'id',label:'name',children:'child',checkStrictly: true}"
            filterDataable
            style="width: 100px"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="街道、社区:" v-if="filterData.household&&filterData.household[0]===1" label-width="90px" style="width: 190px">
          <el-cascader
            v-model="filterData.groups"
            clearable
            placeholder="全部"
            :options="townGroupOption"
            size="small"
            :props="{value:'id',label:'name',children:'child',checkStrictly: true}"
            filterDataable
            style="width: 100px"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="户属性:" label-width="80px" style="width: 235px">
          <el-cascader
            v-model="filterData.monitorPoverty"
            clearable
            placeholder="全部"
            :options="property"
            size="small"
            :props="{value:'id',label:'name'}"
            @change="householdChange"
            filterDataable
            style="width: 150px"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="是否防返贫检测劳动力:" style="width: 270px">
          <el-cascader
            v-model="filterData.isMonitorPoverty"
            clearable
            placeholder="全部"
            :options="wishWorkOption"
            size="small"
            :props="{value:'id',label:'name'}"
            filterDataable
            style="width: 100px"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="提交时间:" label-width="80px" style="width: 460px">
          <el-date-picker
            v-model="time"
            value-format="yyyy-MM-dd HH:mm:ss"
            size="small"
            clearable
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            align="left"
            style="width: 370px"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button size="small" icon="el-icon-search" type="primary" @click="refreshTable">搜索</el-button>
        </el-form-item>
        <el-form-item v-if="user.userType != 4">
          <el-button size="small" icon="el-icon-plus" type="primary" @click="openDetail(null,0)">添加城镇未就业</el-button>
        </el-form-item>
        <el-form-item v-if="user.userType != 4">
          <el-button size="small" icon="el-icon-plus" type="primary" @click="openDetail(null,1)">添加农村劳动力</el-button>
        </el-form-item>
        <el-form-item>
          <el-button size="small" icon="el-icon-download" type="primary" @click="exportRecord">导出列表</el-button>
        </el-form-item>
        <el-form-item>
          <el-button size="small" icon="el-icon-download" type="primary" @click="exportRecordStatistics">导出统计表</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <el-row style="margin-top: 20px">
      <el-table v-loading="tableConf.loading" :stripe="true" :border="true" :data="tableData">
        <el-table-column align="center" width="50px" prop="id" label="ID"/>
        <el-table-column align="center" min-width="100px" prop="realName" label="姓名"/>
        <el-table-column align="center" min-width="100px" prop="sexText" label="性别"/>
        <el-table-column align="center" min-width="100px" prop="age" label="年龄"/>
        <el-table-column align="center" min-width="100px" prop="nationText" label="民族"/>
        <el-table-column align="center" min-width="100px" prop="educationText" label="学历"/>
        <el-table-column align="center" min-width="100px" prop="idCard" label="身份证号码"/>
        <el-table-column align="center" min-width="100px" prop="householdText" label="户籍"/>
        <el-table-column align="center" min-width="100px" prop="cityAddress" label="城市户籍地址"/>
        <el-table-column align="center" min-width="100px" prop="townshipText" label="乡镇(街道)"/>
        <el-table-column align="center" min-width="100px" prop="groupText" label="村组(社区)"/>
        <el-table-column align="center" min-width="100px" prop="mobile" label="联系电话"/>
        <el-table-column align="center" min-width="100px" prop="employmentText" label="就业状态" v-if="user.userType===3"/>
        <el-table-column align="center" min-width="100px" prop="practiceStateText" label="从业状态" v-if="user.userType===3"/>
        <el-table-column align="center" min-width="100px" prop="wishWorkText" label="就业意愿"/>
        <el-table-column align="center" min-width="100px" prop="whetherTrainText" label="培训意愿"/>
        <el-table-column align="center" min-width="100px" prop="veteranText" label="是否退伍军人"/>
        <el-table-column align="center" min-width="100px" prop="returnHomeText" label="是否返乡" v-if="user.userType===3"/>
        <el-table-column align="center" min-width="100px" prop="returnHomeText" label="是否残疾人" v-if="user.userType===2"/>
        <el-table-column align="center" min-width="100px" prop="graduateText" label="是否高校毕业生" v-if="user.userType===2"/>
        <el-table-column align="center" min-width="100px" prop="isHelpText" label="是否已帮扶" />
        <el-table-column align="center" min-width="100px" prop="overcomePovertyText" label="是否脱贫劳动力" />
        <el-table-column align="center" min-width="100px" prop="isMonitorPovertyText" label="是否防返贫检测劳动力" />
        <el-table-column align="center" min-width="100px" prop="monitorPovertyText" label="户属性" />
        <el-table-column align="center" label="是否已就业" width="100px">
          <template slot-scope="{row}">
              <span v-if="row.isWork==1" >是</span>
              <span v-else >否</span>
          </template>
        </el-table-column>
        <el-table-column align="center" fixed="right" width="400px" label="操作">
          <template slot-scope="{row}">
            <el-button size="mini" icon="el-icon-edit-outline" type="primary"  @click="openDetail(row.id,2)">帮扶记录
            </el-button>
            <el-button v-if="row.isWork!=1" size="mini" icon="el-icon-edit-outline" type="primary"  @click="openDetail(row.id,3)">确认就业
            </el-button>
            <el-button v-else size="mini" icon="el-icon-edit-outline" type="primary"  @click="openDetail(row.id,3)">就业去向
            </el-button>
            <el-button v-else size="mini" icon="el-icon-edit-outline" type="primary" v-if="row.household===1" @click="openDetail(row.id,0)">编辑
            </el-button>
            <el-button v-else size="mini" icon="el-icon-edit-outline" type="primary" v-if="row.household===2" @click="openDetail(row.id,1)">编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row style="margin-top: 20px">
      <el-pagination
        :page-sizes="tableConf.sizes"
        :page-size="tableConf.size"
        :total="tableConf.total"
        :current-page="tableConf.page"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-row>
    <el-dialog width="50%"  v-if="visible[0]" :visible.sync="visible[0]" title="添加/编辑" :close-on-click-modal="false"
               :close-on-press-escape="false" :destroy-on-close="true"
    >
      <DetailVillage ref="detail" :id="detailId" :closeDialog="closeDialog"></DetailVillage>
      <span slot="footer" class="dialog-footer">
        <el-button plain type="primary" @click="visible[0] = false">取 消</el-button>
        <el-button type="primary" class="primary" @click="$refs.detail.submit()">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog width="50%"  v-if="visible[1]" :visible.sync="visible[1]" title="添加/编辑" :close-on-click-modal="false"
               :close-on-press-escape="false" :destroy-on-close="true"
    >
      <DetailCity ref="detail" :id="detailId" :closeDialog="closeDialog"></DetailCity>
      <span slot="footer" class="dialog-footer">
        <el-button plain type="primary" @click="visible[1] = false">取 消</el-button>
        <el-button type="primary" class="primary" @click="$refs.detail.submit()">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog width="50%"  v-if="visible[2]" :visible.sync="visible[2]" title="帮扶记录" :close-on-click-modal="false"
               :close-on-press-escape="false" :destroy-on-close="true"
    >
      <DetailHelp ref="detail" :id="detailId" :closeDialog="closeDialog"></DetailHelp>
      <span slot="footer" class="dialog-footer">
        <el-button plain type="primary" @click="closeDialog(2)">取 消</el-button>

      </span>
    </el-dialog>
    <el-dialog width="50%" :append-to-body="true" v-if="visible[3]" :visible.sync="visible[3]" title="帮扶记录" :close-on-click-modal="false"
               :close-on-press-escape="false" :destroy-on-close="true"
    >
      <DetailHelpContent ref="detail" :id="detailId" :state=true :closeDialog="closeDialog"></DetailHelpContent>
      <span slot="footer" class="dialog-footer">
        <el-button plain type="primary" @click="visible[3] = false">取 消</el-button>
        <el-button type="primary" class="primary" @click="$refs.detail.submit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import LivePortendCode from '@/components/LivePortendCode/Detail'
import DetailVillage from '@/components/QuestionCode/DetailVillage'
import DetailCity from '@/components/QuestionCode/DetailCity'
import DetailHelp from '@/components/QuestionCode/DetailHelp'
import DetailHelpContent from '@/components/QuestionCode/DetailHelpContent'
import Cookies from 'js-cookie'
import {
  getAdminQuestionnaireConfigApi,
  getEducationApi,
  getQuestionnaireInvestigationListApi,
  getQuestionnaireInvestigationTablesApi, getQuestionnaireInvestigationStatisticsApi, editIsWorkApi
} from "@/api/QuestionnaireInvestigation";
import {createFile} from "@/utils/utils";
import {deleteJobFairByIdApi} from "@/api/jobFair";


export default {
  components: { LivePortendCode,DetailVillage,DetailCity,DetailHelp,DetailHelpContent },
  data() {
    return {
      user: {},
      tableData: [],
      nationOption: [],
      educationOption: [],
      groupOption:[],
      townGroupOption:[],
      practiceStateOption: [
        {id: 1,name: "务农"},
        {id: 2,name: "就业"}
      ],
      wishWorkOption: [
        {id: 1,name: "是"},
        {id: 2,name: "否"}
      ],
      householdOption: [
        {id: 1,name: "城镇"},
        {id: 2,name: "农村"}
      ],
      property: [
        {id: 1,name: "脱贫不稳定户"},
        {id: 2,name: "边缘易致贫户"},
        {id: 3,name: "突发严重困难户"}
      ],
      tableConf: {
        page: 1,
        sizes: [10, 20, 50, 100],
        total: 0,
        size: 10,
        loading: false
      },
      visible: {//控制弹窗显示
        0: false,
        1: false,
        2: false,
        3: false
      },
      detailId: null,
      filterData: {
        realName: '',
        idCard: '',
        startTime: '',
        endTime: '',
        household:[],
        nationId:[],
        educationId: [],
        practiceState:[],
        groups:[],
        wishWork:[],
        page: 0,
        limit: 0,
        monitorPoverty:[],
        isMonitorPoverty:[],
      },
      time:[],
      placeTypeLoading: true,

    }
  },
  mounted() {
    this.user = JSON.parse(Cookies.get("user"));
    console.log(this.user.userType)
    this.getAdminQuestionnaireConfigList()
    this.getEducationList()
    this.getGroupList()
    this.getTownGroupList()
    this.getTableList()
  },
  methods: {
    downloadCode(url) {
      window.open(url)
    },
    updateRowIsWork(row) {
      const { id, isWork } = row
      console.log(id)
      console.log(isWork)
      editIsWorkApi({ id, isWork: isWork }).then(() => {
        this.$message.success('修改成功')
      }).finally(() => {
        this.getTableList()
      })
    },
    //打开添加/编辑页
    openDetail(id = null,index) {
      this.detailId = id
      this.visible[index] = true
    },
    //关闭指定弹窗并刷新页面
    closeDialog(index) {
      this.visible[index] = false
      this.getTableList()
    },
    // 每页显示数量change
    handleSizeChange(e) {
      this.tableConf.size = e
      this.getTableList()
    },
    // 当前页change
    handleCurrentChange(e) {
      this.tableConf.page = e
      this.getTableList()
    },
    householdChange()
    {
        this.filterData.groups = []
    },

    //获取列表
    getTableList() {
      this.tableConf.loading = true
      const form=JSON.parse(JSON.stringify(this.filterData));
      if(this.time!=null)
      {
        form.startTime = this.time[0];
        form.endTime = this.time[1];
      }
      form.page = this.tableConf.page;
      form.limit = this.tableConf.size;
      form.nationId = this.filterData.nationId.length === 0 ? 0 : this.filterData.nationId[0];
      form.educationId = this.filterData.educationId.length === 0 ? 0 : this.filterData.educationId[0];
      form.practiceState = this.filterData.practiceState.length === 0 ? 0 : this.filterData.practiceState[0];
      form.wishWork = this.filterData.wishWork.length === 0 ? 0 : this.filterData.wishWork[0];
      form.household = this.filterData.household.length === 0 ? 0 : this.filterData.household[0];
      form.monitorPoverty = this.filterData.monitorPoverty.length === 0 ? null : this.filterData.monitorPoverty[0];
      form.isMonitorPoverty = this.filterData.isMonitorPoverty.length === 0 ? null : this.filterData.isMonitorPoverty[0];
      form.groups = this.filterData.groups.length === 0 ? null : this.filterData.groups.join(',');
      getQuestionnaireInvestigationListApi(form).then((res) => {
        this.tableData = res.list
        this.tableConf.total = res.count
      }).finally(() => {
        this.tableConf.loading = false
      })
    },
    //导出当前条件excel
    exportRecord(){
      const form=JSON.parse(JSON.stringify(this.filterData));
      if(this.time!=null)
      {
        form.startTime = this.time[0];
        form.endTime = this.time[1];
      }
      form.nationId = this.filterData.nationId.length === 0 ? 0 : this.filterData.nationId[0];
      form.educationId = this.filterData.educationId.length === 0 ? 0 : this.filterData.educationId[0];
      form.practiceState = this.filterData.practiceState.length === 0 ? 0 : this.filterData.practiceState[0];
      form.wishWork = this.filterData.wishWork.length === 0 ? 0 : this.filterData.wishWork[0];
      form.household = this.filterData.household.length === 0 ? 0 : this.filterData.household[0];
      form.groups = this.filterData.groups.length === 0 ? null : this.filterData.groups.join(',');
      this.$confirm('确定导出当前查询条件的数据吗','提示').then(()=>{
        const loading=this.$loading({ text: '处理中，请勿关闭页面' })
        getQuestionnaireInvestigationTablesApi(form).then((res)=>{
          if(!createFile(res,'问卷调查')){
            this.$message.error('未知的文件格式');
          }
        }).finally(()=>{
          loading.close();
        })
      })
    },

    //导出当前条件excel
    exportRecordStatistics(){
      const form=JSON.parse(JSON.stringify(this.filterData));
      if(this.time!=null)
      {
        form.startTime = this.time[0];
        form.endTime = this.time[1];
      }
      form.nationId = this.filterData.nationId.length === 0 ? 0 : this.filterData.nationId[0];
      form.educationId = this.filterData.educationId.length === 0 ? 0 : this.filterData.educationId[0];
      form.practiceState = this.filterData.practiceState.length === 0 ? 0 : this.filterData.practiceState[0];
      form.wishWork = this.filterData.wishWork.length === 0 ? 0 : this.filterData.wishWork[0];
      form.household = this.filterData.household.length === 0 ? 0 : this.filterData.household[0];
      form.groups = this.filterData.groups.length === 0 ? null : this.filterData.groups.join(',');
      this.$confirm('确定导出当前查询条件的数据吗','提示').then(()=>{
        const loading=this.$loading({ text: '处理中，请勿关闭页面' })
        getQuestionnaireInvestigationStatisticsApi(form).then((res)=>{
          if(!createFile(res,'问卷调查')){
            this.$message.error('未知的文件格式');
          }
        }).finally(()=>{
          loading.close();
        })
      })
    },

    getAdminQuestionnaireConfigList(){
      getAdminQuestionnaireConfigApi({type: 1}).then((res) => {
        this.nationOption = res
      }).finally(() => {
        this.tableConf.loading = false
      })
    },
    getGroupList(){
      getAdminQuestionnaireConfigApi({type: 2}).then((res) => {
        this.groupOption = res
      }).finally(() => {
        this.tableConf.loading = false
      })
    },
    getTownGroupList(){
      getAdminQuestionnaireConfigApi({type: 4}).then((res) => {
        this.townGroupOption = res
      }).finally(() => {
        this.tableConf.loading = false
      })
    },
    getEducationList(){
      getEducationApi().then((res) => {
        this.educationOption = res

      }).finally(() => {
        this.tableConf.loading = false
      })
    },

    refreshTable() {
      this.tableConf.page = 1
      this.getTableList()
    }
  }
}
</script>
<style lang="scss" scoped>
.filterData-block {
  .el-form-item {
    margin-bottom: 0;
  }
}
</style>
